package com.zmn.oms.admin.controller.work.modify;

import com.zmn.common.utils.date.DateUtil;
import com.zmn.oms.admin.controller.OmsBaseController;
import com.zmn.oms.business.interfaces.order.DuplicateOrderBService;
import com.zmn.oms.common.annotation.PageRequestCas;
import com.zmn.oms.common.constant.OrderConsts;
import com.zmn.oms.common.dto.ResultDTO;
import com.zmn.oms.common.exception.OmsBaseException;
import com.zmn.oms.model.dto.work.modify.must.ZsCancelDTO;
import com.zmn.oms.model.entity.changerecord.OrderChangeRecord;
import com.zmn.oms.model.entity.work.OrderWork;
import com.zmn.oms.model.vo.work.modify.must.ZsCancelVO;
import com.zmn.oms.model.vo.work.refund.RefundAmountVO;
import com.zmn.oms.services.interfaces.changerecord.OrderChangeRecordService;
import com.zmn.oms.zmn.business.interfaces.order.OrderVerifyBService;
import com.zmn.oms.zmn.normal.business.interfaces.work.ZsNormalWorkBService;
import com.zmn.pay.common.constant.PayConsts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Objects;

/**
 * 类描述：工单列表主要操作controller
 *
 * @author lujia
 * @date 2018/11/07 16:32
 */
@Controller
@RequestMapping("/work/modify")
public class WorkCancelController extends OmsBaseController {

    @Autowired
    private ZsNormalWorkBService zsNormalWorkBService;
    @Autowired
    private OrderChangeRecordService orderChangeRecordService;
    @Autowired
    private OrderVerifyBService orderVerifyBService;
    @Autowired
    private DuplicateOrderBService duplicateOrderBService;

    /**
     * 取消
     */
    @RequestMapping(value = "/cancel")
    @PageRequestCas(type = OrderConsts.ORDER_OP_TYPE_CANCEL)
    public ModelAndView cancel(Long orderId, Long workId) {
        ModelAndView mv = new ModelAndView("work/modify/cancel");
        ZsCancelVO cancelVO = zsNormalWorkBService.getCancelVO(orderId, workId);
        OrderWork orderWork = cancelVO.getOrderWork();
        // 支付信息（已支付）
        if (Objects.nonNull(orderWork.getMasterAmount()) && Objects.equals(orderWork.getMasterPayStatus(), PayConsts.PAY_STATUS_DONE)
                || Objects.nonNull(orderWork.getPrepayAmount()) && Objects.equals(orderWork.getPrepayStatus(), PayConsts.PAY_STATUS_DONE)
                || Objects.nonNull(orderWork.getChannelPrepayAmount()) && Objects.equals(orderWork.getChannelPrepayStatus(), PayConsts.PAY_STATUS_DONE)
                || Objects.nonNull(orderWork.getDepositAmount()) && Objects.equals(orderWork.getDepositStatus(), PayConsts.PAY_STATUS_DONE)) {
            List<RefundAmountVO> refundList = zsNormalWorkBService.getRefundList(orderWork);
            cancelVO.setRefundList(refundList);
        }

        // 用户发起退款可审核
        if (orderWork.getRefundReviewStatus() == 1 && cancelVO.getRefundList() != null) {
            cancelVO.setIsCheck(true);
        }

        // 查询改约改派次数
        OrderChangeRecord orderChangeRecord = orderChangeRecordService.getChangeRecordByOrderId(orderId);

        String tips = orderVerifyBService.getAmountTips(orderId, workId);

        mv.addObject("tips", tips);

        mv.addObject("cancelList", cancelVO.getCancelList());
        mv.addObject("cancelVO", cancelVO);
        mv.addObject("orderChangeRecord", orderChangeRecord);
        return mv;
    }

    /**
     * 保存取消
     */

    @RequestMapping(value = "/save/cancel")
    @ResponseBody
    public ResultDTO saveCancel(@RequestBody ZsCancelDTO zsCancelDTO) {
        try {
            Integer reviewStatus = zsCancelDTO.getReviewStatus();
            zsCancelDTO.setOperateTime(DateUtil.getNow());
            if (Objects.equals(reviewStatus, OrderConsts.REVIEW_STATUS_FAIL)) {
                // 审核失败只保存备注
                zsNormalWorkBService.saveUnCancel(zsCancelDTO);
            } else {
                zsNormalWorkBService.saveCancel(zsCancelDTO);
            }
            return ResultDTO.success();
        } catch (OmsBaseException e) {
            return ResultDTO.fail(e.getCode(), e.getMessage());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return ResultDTO.fail(e.getMessage());
        }
    }

    /**
     * 检查重复单
     */
    @RequestMapping(value = "/ajax/verifyCancelOrderDuplicate")
    @ResponseBody
    public ResultDTO verifyCancelOrderDuplicate(Long orderId, Long workId, Long duplicateCode) {
        ResultDTO resultDTO = duplicateOrderBService.verifyCancelOrderDuplicate(orderId, workId, duplicateCode);
        return resultDTO;
    }
}
